var { getLoginedUser, formatTime,fnLoading, fnHideLoading, fnSuccess, fnFail} = require('../../utils/index')
import {get} from "../../utils/request.js";
import {payOrder} from "../../utils/util.js";
Page({
  getOrderStatus(status){
      var text = '';
      if (status == 0) {
        text = '执行'
      } else if (status == 1) {
        text = '执行'
      } else if (status == 2) {
        text = '材料准备阶段'
      } else if (status == 3) {
        text = '材料提交阶段'
      } else if (status == 4) {
        text = '国家审核中'
      } else if (status == 5) {
        text = '出证'
      } else if (status == 6) {
        text = '驳回'
      } else if (status == 8) {
        text = '完成'
      } else if (status == '-2') {
        text = '超时未付款取消'
      } else if (status == '-1') {
        text = '取消[付款回退]'
      }
      return {
        status: status,
        text: text
      }
  },
    /**
     * 页面的初始数据
     */
    data: {
        winWidth: 0,
        winHeight: 0,
        pageSize: 10,
        pageNumber: 1,
        order: [],
        navTabIndex: 0,
        totalPage: 0,
        nickName: "",
        groupCode: "",
        spuCanvas: !0,
        actionSheetHidden: !0,
        canvasImg2: "",
        spuCanvasWidth: "",
        spuCanvasHeight: "",
        orders: [],
        pno: 0,
        pageSize: 7,
    },
    navTabIndex: function (e) {
        var t = e.currentTarget.dataset.id, a = this;
        t == a.data.navTabIndex || (a.setData({
            navTabIndex: t,
            pageNumber: 1,
            order: []
        }));
        a.loadMore({status: 
        
        a.data.navTabIndex==0?'0':(
            a.data.navTabIndex == 1 ? '1':
            (
                a.data.navTabIndex == 2 ? '2':'3'
            )
        )
        },0);
    },
    aply: function (e) {
        let that = this;
        fnLoading('支付中...');
        setTimeout(function () {
            fnHideLoading()
        }, 1000)
        var id = e.target.dataset.index;
        var outTradeNo = that.data.orders[id].no;
          debugger;
      var payTotal = that.data.orders[id].total;
        
        var body = that.data.orders[id].remark || '闻权企服-服务订单支付'
        return payOrder({
          url: "/order/reUnifiedOrder",
            outTradeNo, body, payTotal
        }).then(function (payRes) {
            console.log(payRes);
            that.loadMore(that.data.options,0);
            fnSuccess("失败")
        })

    },

    loadMore: function (options,page) {
        let that = this;
        options = options || {};
      var p = page==0?1:that.data.pno + 1;
        fnLoading('加载中...')
        get({
          path: '/order/queryOrder?businessId=1&orderStatus=' + (options.status || '') + '&page=' + p,
        })
            .then(function (res) {
                res = res.map(item => {
                    item.createAt = formatTime(item.createAt)
                    console.log(item.createAt);
                  item.statusLabel = that.getOrderStatus(item.status).text;
                    return item;
                });
              var nrows = page == 0 ? res:that.data.orders.concat(res);
                that.setData({
                    orders: nrows,
                    options: options,
                    pno: p
                })
                fnHideLoading();
                wx.stopPullDownRefresh();
            })
            .catch(function (err) {
            })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.loadMore(options);
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        this.loadMore(this.data.options,0);
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        this.loadMore(this.data.options);
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})